<?php
/**
 * Created by PhpStorm.
 * User: wdt
 * Date: 2016/3/21
 * Time: 16:34
 */

namespace front\modules\web\controllers;


use coupons\services\CouponsGroupService;
use front\controllers\WebBaseController;
use yii\helpers\ArrayHelper;

class CouponsController extends WebBaseController {

    public $layout = 'main';

    /**
     * 获取卡券信息即获取卡券组信息
     */
    public function actionGetInfo()
    {
        $coupons_group_id = \Yii::$app->request->post('coupons_group_id');
        if (empty($coupons_group_id)) {
            WebBaseController::getAjaxData(2,'未传递卡券组ID');
        }
        $ret = CouponsGroupService::getGroupInfo($coupons_group_id);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'获取卡券信息成功');
        }
        WebBaseController::getAjaxData(3,'获取卡券信息失败');
    }

    /**
     * 领取卡券
     */
    public function actionReceive()
    {
        $coupons_group_id = \Yii::$app->request->get('coupons_group_id');
        $user_id = \Yii::$app->request->get('user_id');
//        $user_id = 10000;
        if (empty($coupons_group_id)) {
            WebBaseController::getAjaxData(2,'未传递卡券组ID');
        }
        if (empty($user_id)) {
            WebBaseController::getAjaxData(3,'未传递用户ID');
        }
        $ret = CouponsGroupService::receive($coupons_group_id,$user_id);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'领取卡券成功');
        }
        WebBaseController::getAjaxData(4,'领券卡券失败');
    }

    /**
     * 获取用户不同状态卡券列表
     */
    public function actionList()
    {
        $user_id = $this->getUserId();
        $status = 3;
        if (!empty($_GET['status'])) {
            $status = $_GET['status'];
        }
        if (empty($user_id)) {
            WebBaseController::getAjaxData(2,'未传递用户ID');
        }
        if (empty($status)) {
            WebBaseController::getAjaxData(3,'未传递卡券状态');
        }
        //获取用户获取的卡券信息
        $ret = CouponsGroupService::getStatus($user_id,$status);
        if ($ret['code'] == 1) {
            return $this->render('list', [
                'coupons' => '',
            ]);
        }
        //根据卡券组id,获取卡券活动的具体信息
        $groupInfo = [];
        foreach ($ret['message'] as $k=>$v) {
            $info = CouponsGroupService::getGroupInfo($v['group_id']);
            $data['type'] = $info['result'][0]['type'];
            $data['coupons_status'] = $v['status'];
            $data['coupons_id'] = $v['coupons_id'];
            $data['groupInfo'] = $info['result'][0];
            array_push($groupInfo,$data);
        }
        //对象转换为数组
        $groupInfo = ArrayHelper::toArray($groupInfo);
        if ($ret['code'] == 0) {
            return $this->render('list', [
                    'coupons' => $groupInfo,
            ]);
        }
        WebBaseController::getAjaxData(4,'获取用户卡券列表失败');
    }


    /**
     * 卡券列表页面
     * ajax请求获已使用的卡券
     */
    public function actionAjaxList()
    {
        //从cookie获取用户ID
//      $user_id = $this->user_id;
        $user_id = 123456;
        $type = \Yii::$app->request->get('type');
        //已使用
        if ($type == 1) {
            //获取用户获取的卡券信息
            $ret = CouponsGroupService::getStatus($user_id,4);
            //根据卡券组id,获取卡券活动的具体信息
            $groupInfo = [];
            foreach ($ret['message'] as $k=>$v) {
                $info = CouponsGroupService::getGroupInfo($v['group_id']);
                $data['type'] = $info['result'][0]['type'];
                $data['coupons_status'] = $v['status'];
                $data['coupons_id'] = $v['coupons_id'];
                $data['groupInfo'] = $info['result'][0];
                array_push($groupInfo,$data);
            }
            //对象转换为数组
            $groupInfo = ArrayHelper::toArray($groupInfo);
            $ret = [];
            //转换成前台需要的数据格式，返回到前台ajax请求
            foreach ($groupInfo as $k=>$v) {
                $arr = [];
                if ($v['type'] == 0 || $v['type'] == 1) {
                    $arr['class'] = 3;
                    $arr['type'] = '折扣券';
                    $arr['money'] = $v['groupInfo']['discount']['value'];
                }
                if ( $v['type'] == 2 || $v['type'] == 3) {
                    $arr['class'] = 3;
                    $arr['type'] = '代金券';
                    $arr['money'] = $v['groupInfo']['voucher']['value'];
                }
                if ($v['type'] == 4 || $v['type'] == 5) {
                    $arr['class'] = 3;
                    $arr['type'] = '兑换券';
                    $arr['money'] = 200;
                }
                $arr['desc'] = $v['groupInfo']['describle'];
                $arr['date'] = '有效期至'.date('Y-m-d',$v['groupInfo']['end_time']);
                $arr['leave'] = -1;
                $ret[$v['coupons_id']] = $arr;
            }
            echo trim($_GET['callback']).'('.json_encode($ret).');';
        }

        //未使用
        if ($type == 0) {
            $user_id = 10006722;
            //获取用户获取的卡券信息
            $ret = CouponsGroupService::getStatus($user_id,3);
            //根据卡券组id,获取卡券活动的具体信息
            $groupInfo = [];
            foreach ($ret['message'] as $k=>$v) {
                $info = CouponsGroupService::getGroupInfo($v['group_id']);
                $data['type'] = $info['result'][0]['type'];
                $data['coupons_status'] = $v['status'];
                $data['coupons_id'] = $v['coupons_id'];
                $data['groupInfo'] = $info['result'][0];
                array_push($groupInfo,$data);
            }
            //对象转换为数组
            $groupInfo = ArrayHelper::toArray($groupInfo);
//            print_r($groupInfo); die;
            $ret = [];
            //转换成前台需要的数据格式，返回到前台ajax请求
            foreach ($groupInfo as $k=>$v) {
                $arr = [];
                if ($v['type'] == 0 || $v['type'] == 1) {
                    $arr['class'] = 1;
                    $arr['type'] = '折扣券';
                    $arr['money'] = $v['groupInfo']['discount']['value'];
                }
                if ( $v['type'] == 2 || $v['type'] == 3) {
                    $arr['class'] = 2;
                    $arr['type'] = '代金券';
                    $arr['money'] = $v['groupInfo']['voucher']['value'];
                }
                if ($v['type'] == 4 || $v['type'] == 5) {
                    $arr['class'] = 3;
                    $arr['type'] = '兑换券';
                    $arr['money'] = 200;
                }
                $arr['desc'] = $v['groupInfo']['describle'];
                $arr['date'] = '有效期至'.date('Y-m-d',$v['groupInfo']['end_time']);
                $time = $v['groupInfo']['end_time'] - time();
                $arr['leave'] =intval(($time/86400));
                $ret[$v['coupons_id']] = $arr;
            }
            echo trim($_GET['callback']).'('.json_encode($ret).');';
        }
        WebBaseController::getAjaxData(3,'failed');
    }


    /**
     * 用户使用卡券
     */
    public function actionUse()
    {
        $data = \Yii::$app->request->get();
        if (!isset($data['user_id'])) {
            WebBaseController::getAjaxData(2,'未传递用户id');
        }
        if (!isset($data['order_id'])) {
            WebBaseController::getAjaxData(3,'未传递订单号');
        }
        if (!isset($data['coupons_id'])) {
            WebBaseController::getAjaxData(4,'未传递卡券ID');
        }
        $ret = CouponsGroupService::pay($data['user_id'],$data['coupons_id'],$data['order_id']);
        if ($ret['code'] == 0) {
            WebBaseController::getAjaxData(1,'使用卡券成功');
        }
        WebBaseController::getAjaxData(5,$ret['message']);
    }

}


